tool
class_name Entities, "res://ecs/icons/multi.svg"
extends Notify

const component_name := "Entities"

var entities := []
var entities_map := {}
var _index = 0


func has(id: int) -> bool:
	return entities.has(id)


# get the entity instance
func get(id):
	var entity = entities_map.get(id)
	if not entity:
		entity = .get(id)
	return entity

func iter():
	return entities


func insert(id, entity):
	entities_map[id] = entity


func create(entity_id = null):
	if entity_id == null:
		_index = loop_add(_index)
	else:
		_index = entity_id

	entities.append(_index)
	create_event(_index)
	return _index


func destroy(id: int):
	if has(id):
		entities.erase(id)
		destroy_event(id)


func loop_add(index: int) -> int:
	if index >= 9007199254740992:
		return 1
	else:
		return index + 1
